//
//  DBRetweetedView.swift
//  大波微博swift
//
//  Created by 大波 on 16/3/5.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit
import SnapKit

class DBRetweetedView: UIView {
    
    private let DBRetweetedMargin: CGFloat = 8.0
    
    private var bottonConstrain: Constraint?
    
    var retweetedStatuse: DBStatuses? {
        
        didSet {
            
//            textLabel.text = "test"
            
            let name = retweetedStatuse?.user?.screen_name ?? "没有名字"
            
            let text = retweetedStatuse?.text ?? "没有内容"
            
            textLabel.text = "@\(name): \(text)"
            
            // 判断有没有图片
            bottonConstrain?.uninstall()
            
            if let _ = retweetedStatuse?.picDataURL {
                
                pictureView.hidden = false
                
                pictureView.statuse = retweetedStatuse
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottonConstrain = make.bottom.equalTo(pictureView).offset(DBRetweetedMargin).constraint
                })
                
            } else {
             
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottonConstrain = make.bottom.equalTo(textLabel).offset(DBRetweetedMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: CGRectZero)
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        addSubview(bgButton)
        
        addSubview(textLabel)
        
        addSubview(pictureView)
        
        // 背景按钮
        bgButton.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self)
        }
        
        // 正文
        textLabel.snp_makeConstraints { (make) -> Void in
            
//            make.top.leading.equalTo(self).offset(DBRetweetedMargin)
//            
//            make.trailing.equalTo(self).offset(-DBRetweetedMargin)
            
            
            make.leading.top.equalTo(self).offset(DBRetweetedMargin)
            make.trailing.equalTo(self).offset(-DBRetweetedMargin)
        }
        
        // 图片
        pictureView.snp_makeConstraints { (make) -> Void in
            
//            make.leading.equalTo(self).offset(DBRetweetedMargin)
//            
//            make.top.equalTo(textLabel.snp_bottom).offset(DBRetweetedMargin)
//            
//            make.size.equalTo(CGSize(width: 100, height: 100))
            
            
            make.leading.equalTo(self).offset(DBRetweetedMargin)
            make.top.equalTo(textLabel.snp_bottom).offset(DBRetweetedMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
//            make.bottom.equalTo(pictureView).offset(DBRetweetedMargin)
            
            
            self.bottonConstrain = make.bottom.equalTo(pictureView).offset(DBRetweetedMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    
    /// 背景按钮
    lazy var bgButton: UIButton = {
        
        let bgBtn = UIButton()
        
        bgBtn.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        
        bgBtn.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return bgBtn
    }()
    
    /// 被转发微博名
    private lazy var textLabel: FFLabel = {
        
        let text = FFLabel()
        
        text.numberOfLines = 0
        
        text.labelDelegate = self
        
        return text
    }()
    
    /// 被转发微博图片
    private lazy var pictureView: DBPictureView = DBPictureView()
}


// MARK: - FFLabelDelegate 代理

extension DBRetweetedView: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
//        print("labelDidSelectedLinkText 的 text: \(text)")
        
        if text.hasPrefix("http") {
            
            NSNotificationCenter.defaultCenter().postNotificationName(DBLoadLinkNotification, object: self, userInfo: [DBLoadLinkKey : text])
        }
    }
}















